<template>
	<xn-form-container
		:title="formData.id ? '编辑品牌' : '增加品牌'"
		:width="700"
		:visible="visible"
		:destroy-on-close="true"
		@close="onClose"
	>
		<a-form ref="formRef" :model="formData" :rules="formRules" layout="horizontal">
			<a-form-item label="品牌名称：" name="brandName">
				<a-input v-model:value="formData.brandName" placeholder="请输入品牌名称" allow-clear />
			</a-form-item>
			<a-form-item label="检索关键字：" name="firstLetter">
				<a-input v-model:value="formData.firstLetter" placeholder="请选择检索关键字" allow-clear />
			</a-form-item>
			<a-form-item label="品牌描述：" name="brandDesc">
				<a-textarea :rows="8" v-model:value="formData.brandDesc" placeholder="请输入品牌描述" allow-clear />
			</a-form-item>
			<a-form-item v-if="formData.id||formData.imgUrl" label="更换LOGO：" name="isChangeImg">
				<a-radio v-model:checked="formData.isChangeImg">重新上传</a-radio>
			</a-form-item>

			<a-form-item label="品牌logo图片：" name="currFile">
				<div class="clearfix">
					<a-upload
						v-model:file-list="fileList"
						:beforeUpload="beforeUpload"
						list-type="picture-card"
						@preview="handlePreview"
					>
						<div v-if="fileList.length < 1">
							<plus-outlined />
							<div style="margin-top: 8px">Upload</div>
						</div>
					</a-upload>
					<a-modal :open="previewVisible" :title="previewTitle" :footer="null" @cancel="handleCancel">
						<img alt="example" style="height:800px;width: 100%" :src="previewImage" />
					</a-modal>
				</div>
			</a-form-item>

			<a-form-item label="排序：" name="sort">
				<a-input v-model:value="formData.sort" placeholder="请输入排序" allow-clear />
			</a-form-item>
			<a-form-item label="状态：" name="brandStatus">
				<a-select v-model:value="formData.brandStatus" placeholder="请选择状态 正常 违规"
						  :options="brandStatusOptions" />
			</a-form-item>
		</a-form>
		<template #footer>
			<a-button style="margin-right: 8px" @click="onClose">关闭</a-button>
			<a-button type="primary" @click="onSubmit" :loading="submitLoading">保存</a-button>
		</template>
	</xn-form-container>
</template>

<script setup name="bizBrandForm">
import tool from "@/utils/tool";
import { cloneDeep } from "lodash-es";
import { required } from "@/utils/formRules";
import bizBrandApi from "@/api/biz/bizBrandApi";
import { message } from "ant-design-vue";
// 抽屉状态
const visible = ref(false);
const emit = defineEmits({ successful: null });
const formRef = ref();
// 表单数据
const formData = ref({});
const submitLoading = ref(false);
const brandStatusOptions = ref([]);

// 打开抽屉
const onOpen = (record) => {
	visible.value = true;
	if (record) {
		let recordData = cloneDeep(record);
		formData.value = Object.assign({}, recordData);
	}
	brandStatusOptions.value = tool.dictList("BRAND_STATUS");
};
// 关闭抽屉
const onClose = () => {
	formRef.value.resetFields();
	formData.value = {};
	visible.value = false;
};

// 默认要校验的
const formRules = {
	brandName: [required('请输入品牌名称')],
	brandDesc: [required('请输入品牌描述')],
	sort: [required('请输入排序')],
	brandStatus: [required('请选择状态')],
	firstLetter:[required('请选择检索关键字')],
};
// 验证并提交数据
const onSubmit = () => {
	formRef.value.validate().then(() => {
		let id = formData.value.id;
		submitLoading.value = true;
		const res = new FormData();
		// 新增
		if (id==null){
			res.append("imgUrl",currFile.value)
		}
		// 更新
		else {
			res.append("isChangeImg", formData.value.isChangeImg?"YES":"NO")
			if (formData.value.isChangeImg){
				res.append("imgUrl",currFile.value)
			}
		}
		res.append('firstLetter', formData.value.firstLetter);
		res.append('brandName', formData.value.brandName);
		res.append("brandDesc",formData.value.brandDesc)
		res.append("sort",formData.value.sort)
		res.append("id",id)
		res.append("brandStatus",formData.value.brandStatus)
		bizBrandApi
			.bizBrandSubmitForm(res, id)
			.then(() => {
				onClose();
				emit("successful");
			})
			.finally(() => {
				submitLoading.value = false;
			});
	});
};
// 抛出函数
defineExpose({
	onOpen
});


const beforeUpload = (file) => {
	const isPNG =( file.type === 'image/png'||  file.type === 'image/jpeg')
	if (!isPNG) {
		message.error(`${file.name}暂且只支持png和jpg图片格式`)
		return false
	}
	currFile.value=file
	fileList.value = [...(fileList.value || []), file];
	return false;
}
function getBase64(file) {
	return new Promise((resolve, reject) => {
		const reader = new FileReader();
		reader.readAsDataURL(file);
		reader.onload = () => resolve(reader.result);
		reader.onerror = error => reject(error);
	});
}
const previewVisible = ref(false);
const previewImage = ref('');
const previewTitle = ref('');
// 文件上传
const fileList = ref([]);
const handleCancel = () => {
	previewVisible.value = false;
	previewTitle.value = '';
};
const currFile =ref(null)

const handlePreview = () => {
	previewVisible.value = true;
	getBase64(currFile.value).then(x=>{
		previewImage.value=x
		previewTitle.value = currFile.value.name;
	})
};
</script>
